import { request } from '@umijs/max';
import { setAuthHeader } from '@/utils/authInterceptor';

/**
 * 获取跳闸记录列表（分页）
 * @param params 查询参数
 * @returns 跳闸记录列表数据
 */
export async function getTrippingRecordListPaginated(params: DORM_TRIPPING_RECORD.TrippingRecordListParams) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request<DORM_TRIPPING_RECORD.TrippingRecordListResponse>('/api/dormTrippingRecord/pagedList', {
    method: 'GET',
    headers,
    params,
  });
}

/**
 * 添加跳闸记录
 * @param params 跳闸记录信息
 * @returns 添加结果
 */
export async function addTrippingRecord(params: Omit<DORM_TRIPPING_RECORD.TrippingRecordModel, 'id' | 'createTime' | 'updateTime'>) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/dormTrippingRecord/add', {
    method: 'POST',
    headers,
    data: params,
  });
}

/**
 * 更新跳闸记录信息
 * @param params 跳闸记录信息
 * @returns 更新结果
 */
export async function updateTrippingRecord(params: Partial<DORM_TRIPPING_RECORD.TrippingRecordModel> & { id: number }) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/dormTrippingRecord/update', {
    method: 'PUT',
    headers,
    data: params,
  });
}

/**
 * 删除跳闸记录
 * @param id 跳闸记录ID
 * @returns 删除结果
 */
export async function removeTrippingRecord(id: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/dormTrippingRecord/delete/${id}`, {
    method: 'DELETE',
    headers,
  });
}

/**
 * 获取跳闸记录详情
 * @param id 跳闸记录ID
 * @returns 跳闸记录详情
 */
export async function getTrippingRecordDetail(id: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request<DORM_TRIPPING_RECORD.TrippingRecordDetailResponse>(`/api/dormTrippingRecord/${id}`, {
    method: 'GET',
    headers,
  });
}

/**
 * 按处理状态查询跳闸记录
 * @param isHandled 处理状态（0-未处理，1-已处理）
 * @returns 跳闸记录列表
 */
export async function getTrippingRecordByStatus(isHandled: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/dormTrippingRecord/by-status/${isHandled}`, {
    method: 'GET',
    headers,
  });
}

/**
 * 按宿舍号查询跳闸记录
 * @param dormitoryNum 宿舍号
 * @returns 跳闸记录列表
 */
export async function getTrippingRecordByDormitory(dormitoryNum: string) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/dormTrippingRecord/by-dormitory/${dormitoryNum}`, {
    method: 'GET',
    headers,
  });
}

/**
 * 按班级查询跳闸记录
 * @param className 班级名称
 * @returns 跳闸记录列表
 */
export async function getTrippingRecordByClass(className: string) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/dormTrippingRecord/by-class/${className}`, {
    method: 'GET',
    headers,
  });
}

/**
 * 按辅导员查询跳闸记录
 * @param counselor 辅导员姓名
 * @returns 跳闸记录列表
 */
export async function getTrippingRecordByCounselor(counselor: string) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/dormTrippingRecord/by-counselor/${counselor}`, {
    method: 'GET',
    headers,
  });
}